package com.beyondsoft.sabg.edu.controller.dataanalysis;

import com.beyondsoft.sabg.edu.model.dto.SynonymDto;
import com.beyondsoft.sabg.edu.model.vo.core.SimpleResponseVO;
import com.beyondsoft.sabg.edu.service.dataanalysis.SynonymService;
import com.beyondsoft.sabg.edu.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 同义词控制器
 */
@RestController
@RequestMapping("/api/dataanalysis/synonyms")
@Slf4j
public class SynonymController {

    @Autowired
    private SynonymService synonymService;

    /**
     * 创建同义词
     */
    @PostMapping
    public SimpleResponseVO createSynonym(@Valid @RequestBody SynonymDto synonymDto) {
        log.info("创建同义词请求，主词：{}", synonymDto.getWord());
        
        try {
            String result = synonymService.createSynonym(synonymDto);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("创建同义词失败", e);
            return ResponseUtil.failed("创建同义词失败：" + e.getMessage());
        }
    }

    /**
     * 获取同义词列表
     */
    @GetMapping
    public SimpleResponseVO listSynonyms() {
        log.info("获取同义词列表请求");
        
        try {
            Object result = synonymService.listSynonyms();
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("获取同义词列表失败", e);
            return ResponseUtil.failed("获取同义词列表失败：" + e.getMessage());
        }
    }

    /**
     * 修改同义词
     */
    @PutMapping
    public SimpleResponseVO updateSynonym(@Valid @RequestBody SynonymDto synonymDto) {
        log.info("修改同义词请求，ID：{}", synonymDto.getSynonymIdKey());
        
        try {
            String result = synonymService.updateSynonym(synonymDto);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("修改同义词失败", e);
            return ResponseUtil.failed("修改同义词失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除同义词
     */
    @DeleteMapping("/batch")
    public SimpleResponseVO batchDeleteSynonyms(@RequestBody List<String> synonymIdKeys) {
        log.info("批量删除同义词请求，数量：{}", synonymIdKeys.size());
        
        try {
            String result = synonymService.batchDeleteSynonyms(synonymIdKeys);
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("批量删除同义词失败", e);
            return ResponseUtil.failed("批量删除同义词失败：" + e.getMessage());
        }
    }
} 